package library.common.util;

import android.content.Context;
import android.text.TextUtils;
import android.widget.Toast;

/**
 * @author Created by qiang.hou on 2018/3/20.
 * @version 1.0
 */
public class ToastUtils {

    private static String oldMsg = "";
    private static long oneTime = 0;
    private static long twoTime = 0;
    private static long default_short_duration = 4000;
    private static long default_long_duration = 7000;

    public static void showToast(Context mContext, String message, int showTime) {
        if (!TextUtils.isEmpty(message)) {
            twoTime = System.currentTimeMillis();
            if (!oldMsg.equals(message) || twoTime - oneTime > (showTime == Toast.LENGTH_SHORT ? default_short_duration : default_long_duration)) {
                oldMsg = message;
                oneTime = twoTime;
                Toast.makeText(mContext, message, showTime).show();
            }
        }
    }

    public static void showToast(Context mContext, String message, int showTime, int gravity) {
        if (!TextUtils.isEmpty(message)) {
            twoTime = System.currentTimeMillis();
            if (!oldMsg.equals(message) || twoTime - oneTime > (showTime == Toast.LENGTH_SHORT ? default_short_duration : default_long_duration)) {
                oldMsg = message;
                oneTime = twoTime;
                Toast toast = Toast.makeText(mContext, message, showTime);
                toast.setGravity(gravity, 0, 0);
                toast.show();
            }
        }
    }
}
